//
//  ZlSecondViewController.m
//  TripTogether
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "ZlSecondViewController.h"
#import "WJQNetwork.h"
#import "SizeForLabel.h"
#import "ZLSecondModel.h"
#import "ZLCommentTableViewCell.h"

@interface ZlSecondViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)NSMutableArray *myArray;

@end

@implementation ZlSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor purpleColor];
    self.myArray = [NSMutableArray array];
    [self createTableView];
    //http://api.breadtrip.com/destination/place/5/2388329328/tips/?start=0&tip_id=
    
    
    [self getData];
    


    
}
#pragma mark - 创建TableView
- (void)createTableView
{
    self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.myTableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
//    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.edges.equalTo(self.view);
//    }];

    [self.myTableView registerClass:[ZLCommentTableViewCell class] forCellReuseIdentifier:@"ZLCommentTableViewCell"];
}
#pragma mark - tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInd = @"ZLCommentTableViewCell";
    ZLCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInd];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ZLSecondModel *secondM = [self.myArray objectAtIndex:indexPath.row];
    cell.secondM = secondM;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZLSecondModel *secondM = [self.myArray objectAtIndex:indexPath.row];
    return 70 + secondM.height;
}

#pragma mark - 请求数据
- (void)getData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/tips/?start=0&tip_id=",self.type,self.myId];
    
    [WJQNetwork getAFNet:urlStr block:^(id result) {

        NSMutableDictionary *Dic = result;
        NSMutableArray *tipsArr = [Dic objectForKey:@"tips"];
        
        for (NSMutableDictionary *dic in tipsArr) {
            ZLSecondModel *zlsecondM = [ZLSecondModel new];
            [zlsecondM setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:zlsecondM];

//#pragma mark - 自适应trip_text的高度
            CGSize digestHeight = [SizeForLabel labelRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, 1000) LabelText:zlsecondM.content Font:[UIFont systemFontOfSize:16.0f]];
            if (zlsecondM.content.length == 0) {
                zlsecondM.height = 0;
            }else{
                zlsecondM.height = digestHeight.height;
            }
        }
        
        [self.myTableView reloadData];
    } errorBlock:^(NSError *error) {
        
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
